package com.bizfx.component.presenters
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import com.bizfx.component.fields.IField;
	import com.bizfx.component.views.IDetailView;
	import com.bizfx.component.views.IView;
	import com.bizfx.component.views.ListDetailViewCommand;
	import com.bizfx.component.views.ViewCommand;
	import com.bizfx.errors.NoAlertError;
	import com.bizfx.events.ViewCommandEvent;
	import com.bizfx.events.ViewEvent;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ObjectProxy;

	public class BaseDetailViewPresenter extends BasePresenter {
		
		override public function set view(value:IView):void {
			if (value == super.view) {
				return;
			}
			super.view = value;
			if (this.view) {
				this.view.addEventListener(ViewEvent.View_Loaded, viewLoadedHandler);
			}
		}
		
		public function BaseDetailViewPresenter() {
			super();
		}
		
		private function get thisView():IDetailView {
			return view as IDetailView;
		}
		
		private function initViewData():void {
			if (thisView.initData) {
				this.loadInitViewData();
			} else {
				this.loadViewData();
			}
		}
		
		protected function loadInitViewData():void {
			this.thisView.initData = new ObjectProxy(new Object());
		}

		protected function loadViewData():void {
		}
		/**
		 * Event Handler
		 */
		public function viewLoadedHandler(event:ViewEvent):void {
			thisView.viewDataSource = null;
			this.initViewData();
		}
		
		protected function loadViewDataResponseHandler(event:ResultEvent):void {
			if (event.result.success) {
				this.thisView.viewDataSource = event.result.entity;
			} else {
				Alert.show(event.result.errorMessage, "错 误");
			}
		}
		
		protected function faultResponseHandler(event:FaultEvent):void {
			Alert.show(event.message.toString(), "错 误");
		}
		
		protected function listEditCommandResponseHandler(event:ResultEvent):void {
			if (event.result.success) {
				thisView.viewDataSource = null;
			} else {
				Alert.show(event.result.errorMessage, "错 误");
			}
		}
		
		protected function listAddCommandResponseHandler(event:ResultEvent):void {
			if (event.result.success) {
				thisView.viewDataSource = null;
			} else {
				Alert.show(event.result.errorMessage, "错 误");
			}
		}
		
		protected function listBackGroundProcessCommandResponseHandler(event:ResultEvent):void {
			if (event.result.success) {
				thisView.viewDataSource = null;
			} else {
				Alert.show(event.result.errorMessage, "错 误");
			}
		}
		
		protected function loadInitViewDataResponseHandler(event:ResultEvent):void {
			thisView.initData = event.result;
		}
	}
}